package com.wcfsy.wechat.wx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wcfsy.wechat.wx.entity.AboutInfo;
import com.wcfsy.wechat.wx.entity.ProductType;
import com.wcfsy.wechat.wx.pagination.IPage;
import com.wcfsy.wechat.wx.pagination.impl.Page;
import com.wcfsy.wechat.wx.sys.dao.ProductTypeDao;
import com.wcfsy.wechat.wx.util.DateStyle;
import com.wcfsy.wechat.wx.util.DateUtil;
import com.wcfsy.wechat.wx.util.JsonMessage;

@Controller
@RequestMapping("/cms/productType")
public class ProductTypeController {

	@Resource
	private ProductTypeDao productTypeDao;

	@RequestMapping({ "/view/page" })
	public String page() {
		return "productType/productType_list";
	}

	@RequestMapping({ "/view" })
	@ResponseBody
	public IPage<ProductType> view(int page, int rows) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("index", page);
		paramMap.put("size", rows);
		paramMap.put("limit", "limit");
		return new Page<ProductType>(productTypeDao.findByParam(paramMap),
				productTypeDao.count(paramMap), page, rows);
	}

	@RequestMapping({ "/add/page" })
	public String preAdd() {
		return "productType/productType_add";
	}

	@RequestMapping({ "/add" })
	@ResponseBody
	public JsonMessage add(ProductType productType, HttpServletRequest request) {
		try {
			productType.setCreateDate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			productTypeDao.add(productType);
			return JsonMessage.getSuccessJson("添加成功");
		} catch (Exception e) {
			return JsonMessage.getSuccessJson("添加失败");
		}
	}

	@RequestMapping({ "/delete" })
	@ResponseBody
	public JsonMessage delete(int id) {
		try {
			productTypeDao.deleteById(id);
			return JsonMessage.getSuccessJson("删除成功");
		} catch (Exception e) {
			return JsonMessage.getSuccessJson("删除失败");
		}

	}

	@RequestMapping({ "/modify/page" })
	public ModelAndView preModify(int id) {
		ModelAndView mav = new ModelAndView("productType/productType_modify");
		mav.addObject("data", productTypeDao.getById(id));
		return mav;
	}

	@RequestMapping({ "/modify" })
	@ResponseBody
	public JsonMessage modify(ProductType productType, HttpServletRequest request) {
		try {
			productType.setUpdateDate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			productTypeDao.update(productType);
			return JsonMessage.getSuccessJson("修改成功");
		} catch (Exception e) {
			return JsonMessage.getSuccessJson("修改失败");
		}
	}

	@RequestMapping({ "/add/view" })
	public ModelAndView preView(AboutInfo info) {
		ModelAndView mav = new ModelAndView("productType/productType_view");
		mav.addObject("data", info);
		return mav;
	}
}
